home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1375 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  109 lines

  1. Path: gate.net!pslfl2-25
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Splitting String ?
  5. Date: 12 Jan 1996 23:24:04 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4d6qik$r78@news.gate.net>
  8. References: <HAKOLA.96Jan12151128@jung.hut.fi>
  9. NNTP-Posting-Host: pslfl2-36.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <HAKOLA.96Jan12151128@jung.hut.fi>,
  13.    hakola@cadmail.hut.fi (Petri Hakola) wrote:
  14. >
  15. >    Have I missed something (again:) or why doesn't this code
  16. >    work? I should split dos-a-like-filename and add new postfix
  17. >    instead of old one (i.e. DATA.TXT --> DATA.UPD  It seems to
  18. >    work correctly if the filename has an old postfix, but if
  19. >    there isn't one start won't return what it should.
  20. >
  21. >                            - P -
  22. >
  23. >
  24. >---Clip---
  25. >#include <stdio.h>
  26. >#include <string.h>
  27. >
  28. >char *newname(char *s) {
  29.  
  30. If you're going to be passing pointers to string literals to this function or 
  31. if you don't know that you can legally address chars beyond the end of the 
  32. string it should be defined taking an argument of const char * (for your own 
  33. protection).
  34.  
  35. >
  36. >   char *dot;
  37. >   char *start;
  38.  
  39. Make start a static array, which will allow you to copy the const char *s to 
  40. it for manipulation. Static so that it sticks around after you return to the 
  41. caller.
  42.  
  43. >   char end[5];
  44. >
  45. >   strcpy(end,".UPD");
  46.  
  47. If end is always gonna be equal to ".UPD" you can make it static to save you 
  48. the initialization code each time the function is called.
  49.  
  50. >   start = s;
  51.  
  52. Copy s to start and then work with start which will be oversized to allow 
  53. appending chars.
  54.  
  55. >   dot = strchr(s,'.');
  56. >   if( dot == NULL) {
  57. >     start[strlen(start)] = '\0';
  58. >     dot = end;
  59.  
  60. The above to lines are not needed at all.
  61.  
  62. >   } else {
  63. >   *dot = '\0';
  64. >   dot++;
  65. >   dot = end;
  66.  
  67. Here, you increment dot and then assign it a different value. These two lines 
  68. are unnecessary also.
  69.  
  70. >   }
  71. >   strcat(start, dot);
  72.  
  73. Here, you only need to append end to start.
  74.  
  75. >   return start;
  76. >}
  77. >
  78. >main() {
  79. >  printf("%s\n",newname("LONGNAME.TXT"));
  80.  
  81. Here, your code worked because you replaced 3 letters with 3 letters. Even 
  82. though your system might allow it, it's illegal, and certainly not portable to 
  83. change a string literal.
  84.  
  85. >  printf("%s\n",newname("NOEND"));
  86.  
  87. With this one, where is it going to put ".UPD"?
  88.  
  89. >}
  90.  
  91. char *newname(const char *s)
  92. {
  93.  
  94. char *dot;
  95. static char start[MAX_FILENAME_SIZE];
  96. static const char *end = ".UPD";
  97.  
  98.    strcpy(start,s);          /*copy s to start before any manipulation*/
  99.    dot = strrchr(start,'.');    /*reverse in case multiple '.'s in future*/
  100.    if(dot) 
  101.        *dot = '\0';
  102.    strcat(start,end);
  103.    return start;
  104. }
  105.  
  106. Bill
  107.  
  108. "Whatcha got on?...Your mind?"
  109.